package controllers;

import java.util.List;

import javax.persistence.criteria.Order;

import models.Cart;
import models.CartItem;
import models.Product;
import models.User;
import models.UserOrder;
import play.data.DynamicForm;
import play.data.Form;
import play.data.validation.Constraints.Required;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.authentication.login;
import views.html.cart.cart;

public class CartControl extends Controller {
	
	public static Result cart()
	{
		User user = User.find.where().eq("username", session().get("uname")).findUnique();
		List<CartItem> items;
		if(user != null)
		{
			if(user.cart == null)
			{
				user.cart = Cart.create(user);
				user.save();
				System.out.println("NULL");
			}
			if(session().get("cartid") != null)
			{
				Cart cart = Cart.find.byId(Long.valueOf(session().get("cartid")));
				cart = Cart.mergeCarts(user.cart, cart);
				cart.save();
				user.cart = cart;
				session().remove("cartid");
				System.out.println("merged");
			}
			user.save();
			items = user.cart.items;
			System.out.println("Got it");
		}
		else
		{
			if(session().get("cartid") != null)
			{
				System.out.println(session().get("cartid"));
				Cart cart = Cart.find.byId(Long.valueOf(session().get("cartid")));
				items = cart.items;
			}else{
				Cart tmp = Cart.create(null);
				tmp.isTemp = true;
				tmp.save();
				session("cartid", tmp.id+"");
				items = tmp.items;
			}
		}
		return ok(cart.render(items));
	}
	
	public static Result addToCart()
	{
		DynamicForm form = Form.form().bindFromRequest();
		Long productId = Long.valueOf(form.get("productId"));
		Integer quantity = Integer.valueOf(form.get("quantity"));
		
		User user = User.find.where().eq("username", session().get("uname")).findUnique();
		Cart cart;
		
		if(user == null)
		{
			if(session().get("cartid") == null)
			{
				cart = Cart.create(null);
				cart.isTemp = true;
				cart.save();			
			}else{
				cart = Cart.find.byId(Long.valueOf(session().get("cartid")));
				System.out.println("Found");
			}
		}else{
			cart = user.cart;
			session().remove("cartid");
		}
		
		CartItem item = CartItem.create(Product.find.byId(productId), quantity);
		
		for(int i = 0; i < cart.items.size(); i++)
		{
			if(cart.items.get(i).product.id == productId)
			{
				item = cart.items.get(i);
				
				if(item.quantity + quantity > Product.find.byId(productId).quantity)
					item.quantity = Product.find.byId(productId).quantity;
				else
					item.quantity += quantity;
					
				cart.items.set(i, item);
				cart.save();
				return ok();
			}
		}
		
		cart.items.add(item);
		cart.save();
		
		if(user != null)
		{
			user.cart = cart;
			user.save();
		}
		
		System.out.println("Added");
		return ok();
	}
	
	public static Result updateCart()
	{
		Form<CartForm> form = Form.form(CartForm.class).bindFromRequest();
		return ok();
	}
	
	public static Result removeFromCart(Long id)
	{
		User user = User.find.where().eq("username", session().get("uname")).findUnique();
		Cart cart = user.cart;
		
		for(int i = 0; i < cart.items.size(); i++)
		{
			if(cart.items.get(i).id == id)
			{
				System.out.println("found");
				cart.items.get(i).delete();
				cart.items.remove(i);
				break;
			}
		}
		
		cart.save();
		return ok();
	}
	
	public static Result clean()
	{
		Cart cart = User.find.where().eq("username", session().get("uname")).findUnique().cart;
		cart.items.clear();
		cart.save();
		return ok();
	}
	
	public static Result order()
	{
		DynamicForm form = Form.form().bindFromRequest();
		Long cartId = Long.valueOf(form.get("id"));
		
		User user = User.find.where().eq("username", session().get("uname")).findUnique();
		
		if(user == null)
		{
			flash("fail", "Please log in.");
			return ok(login.render(Form.form(Authentication.Login.class)));
		}else if(user != null && ("".equals(user.address) || user.address == null)){
			flash("fail", "Add address please.");
			session("prev", "order");
			return ok(login.render(Form.form(Authentication.Login.class)));
		}
		
		UserOrder order = UserOrder.create(user);
		order.items = user.cart.items;
		order.status = 0;
		order.save();
		
		user.cart.delete();
		user.cart = Cart.create(user);
		user.cart.save();
		user.save();
		
		return ok();
	}
	
	public static class CartForm
	{
		@Required
		public Long itemId;
		@Required
		public Integer quantity;
	}
}
